#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1
 Author:         Sebastian Eves-van den Akker (University of Leeds)
				SebastianEvesvandenAkker@gmail.com

 Script Function: Measures area of phytopthera infection
#ce ----------------------------------------------------------------------------

HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

Func TogglePause()
  $Paused = NOT $Paused
  While $Paused
    sleep(100)
    ToolTip('Script is "Paused"',0,0)
  WEnd
  ToolTip("")
EndFunc

Func Terminate()
 Exit
EndFunc

global $tip = 0
#Include <Misc.au3>
#include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>
#Include <GUIConstantsEx.au3>
#Include <WindowsConstants.au3>
#Include <WinAPI.au3>
;============================================================================================================

_DwmEnable(False)
;=====================Creates the Graphical User Interface of the Program=================================
$version = "Phyt area v 1.0"
GUICreate($version, 240,200,@DesktopWidth -245,0)

$next = GUICtrlCreateButton("Start",20, 10, 80, 20)
$slider1 = GUICtrlCreateSlider(10, 50, 220, 20,$TBS_TOP)
GUICtrlSetLimit(-1, 200, 1) 
$button = GUICtrlCreateInput("Threshold",175, 70, 50, 20)
$n3 = GUICtrlCreateInput("#",20, 120, 50, 20)
GUICtrlCreateGroup ( "Threshold", 10, 35, 220,60)
GUICtrlCreateGroup ( "Pixel Count - area", 10, 100, 220,45)

GUICtrlCreateGroup ( "Relative size - /leaf area", 10, 150, 220,40)
$coverage = GUICtrlCreateInput("coverage",15,165,90, 20)
GUISetState()
 
$name = "SEvdA"
 $hGUI = GUICreate($name, @desktopwidth+30, @desktopheight+30, -10, -30, -1, BitOR($WS_EX_TOPMOST, $WS_EX_LAYERED))
$label_color = 0x000000
GUISetBkColor(0xABCDEF) ; make gui bk color transparant
_WinAPI_SetLayeredWindowAttributes($hGUI, 0xABCDEF, 150) ; set transparancy
_WinAPI_SetWindowLong($hGUI, -20, BitOR(_WinAPI_GetWindowLong($hGUI, -20), 0x00000020)) ; disable user intput
GUISetStyle($WS_POPUP, -1, $hGUI) ;make gui borderless
$test = guictrlcreatelabel ("",0,0,79,1);top right
$test2 = guictrlcreatelabel ("",0,0,1,79);top right
guictrlsetbkcolor($test,$label_color)
guictrlsetbkcolor($test2,$label_color)
$test3 = guictrlcreatelabel ("",@desktopwidth-79,@desktopheight-1,79,1);bottom
$test4 = guictrlcreatelabel ("",@desktopwidth-1,@desktopheight-79,1,79);bottom
guictrlsetbkcolor($test3,$label_color)
guictrlsetbkcolor($test4,$label_color)
guisetstate()

;========================================Default threshold, 190=====================================
GUICtrlSetData($button, 190)
GUICtrlSetData($slider1, 190)
	
;check only 1 copy exists.
 if _singleton($version,1) = 0 then 
 MsgBox(0, $version, "Running 2 copies of "&$version&" is not supported")
Exit
EndIf
		
Do
	if $tip = 0 then 
	tooltip("Click Start when ready to start",0,0, "To end Press ESC   To pause Press PAUSE-BREAK")
	$tip = 1
	EndIf
	
	$msg = GUIGetMsg()
	
	if $msg = $GUI_EVENT_CLOSE Then
		exit 0
	endif
	
$sliderread = GUICtrlRead($slider1, 1)
GUICtrlSetData($button, $sliderread)

	if $msg = $GUI_EVENT_CLOSE Then
		exit 0
		endif

if $msg = $next Then 
$date = @MDAY & "/" & @MON & "/" & @YEAR
$time = @HOUR & "." & @MIN & "." & @SEC	
$dateminus = stringreplace ($date,"/","_")
$filefordata = $dateminus & "  "  & $time & ".txt"
$filefordata2 = $dateminus & "  "  & $time

$exitloops = 0
$inputbox = InputBox("Folder Name", " ", "Name of Folder here", "", 100, 50,@desktopwidth/2,@desktopheight/2)
 If $inputbox = not "" then
	 dircreate (@workingdir &"/" & $inputbox)
_page()
 EndIf

EndIf
	until $msg = $GUI_EVENT_CLOSE
	
func _page()
	
$tip = 0
Do
tooltip("To start a new photo - Press 1      To exit press ESC",0,0,"Click top left")
do


$pos = mousegetpos()
controlmove($name,"",$test,$pos[0]+7, $pos[1]+5)
controlmove($name ,"",$test2,$pos[0]+7, $pos[1]+5)
if _ispressed (31) then
controlmove($name ,"",$test,0,0)
controlmove($name ,"",$test2,0,0)
controlmove($name ,"",$test3,@desktopwidth, @desktopheight)
controlmove($name ,"",$test4,@desktopwidth, @desktopheight)
$exitloops = 1
exitloop
EndIf
until _ispressed(01)
$array = mousegetpos()

if $exitloops = 0 then sleep(500)
tooltip("To start a new photo - Press 1       To exit press ESC",0,0,"Click bottom right")	
do
	
if $exitloops = 1 then exitloop
	
if _ispressed (31) then
controlmove($name ,"",$test,0,0)
controlmove($name ,"",$test2,0,0)
controlmove($name ,"",$test3,@desktopwidth, @desktopheight)
controlmove($name ,"",$test4,@desktopwidth, @desktopheight)
$exitloops = 1
exitloop
EndIf


sleep(50) 
$pos = mousegetpos()
controlmove($name,"",$test3,$pos[0]-83+13, $pos[1]-4+11)
controlmove($name,"",$test4,$pos[0]-4+13, $pos[1]-83+11)
until _ispressed(01)
$array1 = mousegetpos()
if $exitloops = 1 then
$exitloops = 0
exitloop
endif
$y = $array[1]
$x = $array[0]
$xb = $array1[0]
$yb = $array1[1]

$shades = GUIctrlread($slider1)
$tot = 0
$phy = 0

$xta = $x
$yta = $y
$xba = $xb
$yba = $yb
tooltip("working, wait",0,0,"")
$color = 0xFEFEFE
$c = 0xffffff
$w = 1
$gaad = _WinAPI_GetWindowDC(0)
$gaac = _WinAPI_CreatePen($ps_solid,$w, $c)
$gaab = _WinAPI_SelectObject($gaad, $gaac)

do

	
Do
	$x = $x + 1		
	
	$color8 = pixelgetcolor($x,$y)
							    $hex = Hex ( $color8 , 6)
								$HR = StringMid($hex, 1, 2)
								$HG = StringMid($hex, 3, 2)
								$HB = StringMid($hex, 5, 2)
								$Rx = Dec($HR)
								$Gx = Dec($HG)
								$Bx = Dec($HB)	
								
								if ($rx + $gx+ $bx) / 3 < 230 then $tot = $tot + 1
								
	$pix = pixelsearch($x,$y,$x,$y,$color,$shades) 
	if @error then 
		$phy = $phy + 1
		_WinAPI_DrawLine($gaad,$x,$y,$x+1,$y)
	endif
until $x > $xba
$x = $xta
$y = $y + 1
until $y > $yba

GUIctrlsetdata($coverage,round(($phy/$tot*100),0) & "%")
GUIctrlsetdata($n3,round($phy,0)) 
controlmove($name,"",$test,0,0)
controlmove($name,"",$test2,0,0)
controlmove($name,"",$test3,@desktopwidth, @desktopheight)
controlmove($name,"",$test4,@desktopwidth, @desktopheight)
$date = @MDAY & "/" & @MON & "/" & @YEAR
$time = @HOUR & "." & @MIN & "." & @SEC
$dateminus = stringreplace ($date,"/","_")
Filewrite (@workingdir &"/" & $inputbox & "/" & $filefordata, "Date:"& $date & "     Time:" & $time & " size = " & @tab & $phy & @CRLF)
$phy = 0
sleep(100)
until 1 = 2
EndFunc	
	
	Func _DwmEnable($WhatToDo)
DllCall("dwmapi.dll", "long", "DwmEnableComposition", "uint", $WhatToDo)
EndFunc